﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharePointPowerToys
{
    public class TextLinkEditorSectionEditorPartControl : Control, INamingContainer
    {
        const string FieldEnd = "<div class=\"UserDottedLine\" width=\"100%\"></div>";
        const string SectionEnd = "<div style='width:100%' class='ms-formline'></div>";

        public string Mode { get; set; }

        ButtonHeaderSharePointEditorPartControl textEditor = new ButtonHeaderSharePointEditorPartControl();
        public string Text
        {
            get
            {
                return textEditor.Text;
            }

            set
            {
                textEditor.Text = value;
            }
        }
        LinkSharePointEditorPartControl linkEditor = new LinkSharePointEditorPartControl();
        public string Link
        {
            get
            {
                return linkEditor.Link;
            }

            set
            {
                linkEditor.Link = value;
            }
        }

        public bool Secure { get; set; }

        private TextBoxSharePointEditorPartControl usernameTextBox = new TextBoxSharePointEditorPartControl();

        public string Username
        {
            get
            {
                return usernameTextBox.Text;
            }

            set
            {
                usernameTextBox.Text = value;
            }
        }

        private TextBoxSharePointEditorPartControl passwordTextBox = new TextBoxSharePointEditorPartControl();

        public string Password
        {
            get
            {
                return passwordTextBox.Text;
            }

            set
            {
                passwordTextBox.Text = value;
            }
        }

        public bool ShowCacheSeconds { get; set; }

        private TextBoxSharePointEditorPartControl cacheSecondsTextBox = new TextBoxSharePointEditorPartControl();

        public long CacheSeconds
        {
            get
            {
                return Convert.ToInt64(cacheSecondsTextBox.Text);
            }

            set
            {
                cacheSecondsTextBox.Text = value.ToString();
            }
        }

        private TextBoxSharePointEditorPartControl cacheKeyTextBox = new TextBoxSharePointEditorPartControl();

        public string CacheKey
        {
            get
            {
                return cacheKeyTextBox.Text;
            }

            set
            {
                cacheKeyTextBox.Text = value;
            }
        }

        public TextLinkEditorSectionEditorPartControl(string mode)
        {
            Mode = String.Concat(mode);
            Secure = false;
            ShowCacheSeconds = false;
        }

        public TextLinkEditorSectionEditorPartControl(string mode, bool secure)
        {
            Mode = mode;
            Secure = secure;
            ShowCacheSeconds = false;
        }

        public TextLinkEditorSectionEditorPartControl(string mode, bool secure, bool showCacheSeconds)
        {
            Mode = mode;
            Secure = secure;
            ShowCacheSeconds = showCacheSeconds;
        }

        public TextLinkEditorSectionEditorPartControl(string mode, bool secure, bool showCacheSeconds, string text, string link)
        {
            Mode = mode;
            Secure = secure;
            ShowCacheSeconds = showCacheSeconds;
            Text = text;
            Link = link;
        }
        
        protected override void CreateChildControls()
        {
            try
            {
                textEditor.Header = String.Concat(Mode, " Editor");
                textEditor.Body = String.Concat("To add ", Mode, ", click <b>", Mode, " Editor</b>.<br/>");
                textEditor.ID = String.Concat(Mode, ID, "ButtonHeaderTextBox");
                Controls.Add(textEditor);

                linkEditor.ID = String.Concat(Mode, ID, "LinkEditor");
                linkEditor.Header = String.Concat(Mode, " Link");
                linkEditor.Body = String.Concat("To link to an ", Mode, " file, type a URL.");
                linkEditor.BuilderVisible = true;
                Controls.Add(linkEditor);

                if (Secure)
                {
                    Controls.Add(new LiteralControl(FieldEnd));

                    usernameTextBox.Header = "Security Credentials (if required)";
                    usernameTextBox.Body = "[Domain/]Username:";
                    usernameTextBox.ID = String.Concat(Mode, ID, "UsernameTextBox");
                    usernameTextBox.TextBoxMode = TextBoxMode.Password;
                    Controls.Add(usernameTextBox);

                    Controls.Add(new LiteralControl(FieldEnd));

                    passwordTextBox.Body = "Password:";
                    passwordTextBox.ID = String.Concat(ID, "PasswordTextBox");
                    passwordTextBox.TextBoxMode = TextBoxMode.Password;
                    Controls.Add(passwordTextBox);
                }

                if (ShowCacheSeconds)
                {
                    Controls.Add(new LiteralControl(FieldEnd));

                    cacheSecondsTextBox.Body = String.Concat("Enter Seconds to Cache ", Mode, " file:");
                    cacheSecondsTextBox.ID = String.Concat(ID, "CacheSecondsTextBox");
                    cacheSecondsTextBox.ShowFieldSeparator = false;
                    Controls.Add(cacheSecondsTextBox);

                    cacheKeyTextBox.Body = "Enter Cache Key (Optional):";
                    cacheKeyTextBox.ID = String.Concat(ID, "CacheKeyTextBox");
                    cacheKeyTextBox.ShowFieldSeparator = false;
                    Controls.Add(cacheKeyTextBox);
                }

                Controls.Add(new LiteralControl(SectionEnd));
            }
            catch (Exception ex)
            {
                Controls.Add(new LiteralControl(String.Concat("Error: ", ex.Message)));
            }
        }
    }
}
